﻿package cn.smbms.service.user;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.user.UserDao;
import cn.smbms.dao.user.UserDaoImpl;
import cn.smbms.pojo.User;
import cn.smbms.tools.MyBatisUtil;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	@Qualifier("userDao")
	private UserDao userDao;
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public UserServiceImpl(){
		userDao=new UserDaoImpl();
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean add(User user) {
		// TODO Auto-generated method stub
		boolean flag=false;
		try {
			int updateRows = userDao.add(user);
			if(updateRows > 0){
				flag = true;
				System.out.println("add success!");
			}else{
				System.out.println("add failed!");
			}
			return flag;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public User login(String userCode, String userPassword) {
		// TODO Auto-generated method stub
		User user = null;
		try {
			user = userDao.getLoginUser(userCode);
			//匹配密码
			if(null != user){
				if(!user.getUserPassword().equals(userPassword))
					user = null;
			}
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}	
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		try {
			return userDao.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public User selectUserCodeExist(String userCode) {
		// TODO Auto-generated method stub
		User user = null;
		try {
			user = userDao.getLoginUser(userCode);
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean deleteUserById(Integer delId) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			if(userDao.deleteUserById(delId) > 0){
				flag = true;
			}
			return flag;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	public User getUserById(Integer id) {
		// TODO Auto-generated method stub
		try{
			return userDao.getUserById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw e;
		}
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean modify(User user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			if(userDao.modify(user) > 0){
				flag = true;
			}
			return flag;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean updatePwd(int id, String pwd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			if(userDao.updatePwd(id,pwd) > 0){
				flag = true;
			}
			return flag;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw e;
		}	
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public int getUserCount(String queryUserName, int queryUserRole) {
		// TODO Auto-generated method stub
		try {
			return userDao.getUserCount(queryUserName, queryUserRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
	}
}
